﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FunWithStrings
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with Strings *****");
            Console.WriteLine();

            BasicStringFunctionality();
            Console.WriteLine();
            
            StringConcatenation();
            Console.WriteLine();

            EscapeChars();
            Console.WriteLine();

            StrinEquality();
            Console.WriteLine();

            StringsAreImmutable();
            Console.WriteLine();

            FunWithStringBulider();
            Console.WriteLine();

            Console.ReadLine();
        }

        /// <summary>
        /// 基本的字符操作
        /// </summary>
        private static void BasicStringFunctionality()
        {
            Console.WriteLine("=> Basic String functionality:");
            const string firstName = "Freddy";
            Console.WriteLine("Value of firstName: {0}", firstName);
            Console.WriteLine("firstName has {0} characters.", firstName.Length);
            Console.WriteLine("firstName in uppercase: {0}", firstName.ToUpper());
            Console.WriteLine("firstName in lowercase: {0}", firstName.ToLower());
            Console.WriteLine("firstName contains the letter y?: {0}", firstName.Contains("y"));
            Console.WriteLine("firstName after replace: {0}", firstName.Replace("dy", ""));
            Console.WriteLine();
        }

        /// <summary>
        /// 字符串的拼接
        /// </summary>
        private static void StringConcatenation()
        {
            Console.WriteLine("=> String concatenation:");
            string s1 = "Programming";
            string s2 = "PsychoDrill(PTP)";
            string s3 = string.Concat(s1, s2);
            //string s3 = s1 + s2;
            Console.WriteLine(s3);
            Console.WriteLine();
        }

        /// <summary>
        /// 转义字符
        /// </summary>
        private static void EscapeChars()
        {
            Console.WriteLine("=> Escape characters:\a");
            const string strWithTabs = "Model\tColor\tSpeed\tpet Name\a";
            Console.WriteLine(strWithTabs);

            Console.WriteLine("Everyone loves \"Hello World\"\a");
            Console.WriteLine("C:\\MyApp\\bin\\Debug\a");
            Console.WriteLine(@"C:\MyApp\bin\Debug");

            Console.WriteLine("All finished. \n\n\n\a");
            Console.WriteLine();
        }

        /// <summary>
        /// 字符串的相等性
        /// </summary>
        private static void StrinEquality()
        {
            Console.WriteLine("=> String equality:");
            const string s1 = "Hello!";
            const string s2 = "Yo!";
            Console.WriteLine("s1 = {0}", s1);
            Console.WriteLine("s2 = {0}", s2);
            Console.WriteLine();

            Console.WriteLine("s1 == s2: {0}", s1 == s2);
            Console.WriteLine("s1 == Hello!: {0}", s1 == "Hello!");
            Console.WriteLine("s1 == HELLO!: {0}", s1 == "HELLO!");
            Console.WriteLine("s1 == hello!: {0}", s1 == "hello!");
            Console.WriteLine("s1.Equals(s2): {0}", s1.Equals(s2));
            Console.WriteLine("Yo!.Equals(s2): {0}", "Yo!".Equals(s2));

            Console.WriteLine();
        }

        /// <summary>
        /// 字符串是不可变的
        /// </summary>
        private static void StringsAreImmutable()
        {
            string s1 = "This is my string.";
            Console.WriteLine("s1 = {0}", s1);

            string upperString = s1.ToUpper();
            Console.WriteLine("upperString = {0}", upperString);
            Console.WriteLine("s1 = {0}", s1);
            Console.WriteLine();
        }

        /// <summary>
        /// 字符串的不变性
        /// </summary>
        private static void StringsAreImmutable2()
        {
            string s2 = "My other string.";
            s2 = "New string value";
        }

        /// <summary>
        /// System.Text.StringBuilder类型
        /// </summary>
        private static void FunWithStringBulider()
        {
            Console.WriteLine("=> Using the StringBuilder:");
            StringBuilder sb = new StringBuilder("**** Fantastic Games ****", 256);
            sb.Append("\n");
            sb.AppendLine("Half Life");
            sb.AppendLine("Morrowind");
            sb.AppendLine("Deus Ex" + " 2");
            sb.AppendLine("System Shock");
            Console.WriteLine(sb.ToString());

            sb.Replace("2", "Invisible War");
            Console.WriteLine(sb.ToString());
            Console.WriteLine("sb has {0} chars.", sb.Length);
            Console.WriteLine();
        }

    }
}
